from enum import Enum, IntEnum
from tortoise import fields, models
from passlib.context import CryptContext

# 使用 bcrypt 加密算法
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

class UserRole(IntEnum):
    SUPER_ADMIN = 1 #超管
    RECRUITER = 2 #招聘者
    JOB_SEEKER = 3 #求职者

class JobSeekerStatus(IntEnum):
    UNEMPLOYED_SEEKING = 1  # 离职正在找工作
    EMPLOYED_URGENT = 2     # 在职急需工作
    EMPLOYED_SEEKING = 3    # 在职找工作
    EMPLOYED_NOT_SEEKING = 4 # 在职没有找工作的打算


class User(models.Model):
    "用户模型"
    username = fields.CharField(max_length=12, unique=True, description="用户名")
    role = fields.IntEnumField(UserRole, default=UserRole.JOB_SEEKER, description="用户角色")
    phone = fields.CharField(max_length=11, unique=True, null=True, description="手机号")
    email = fields.CharField(max_length=50, unique=True, description="邮箱")
    password = fields.CharField(max_length=100, description="密码")
    gender = fields.CharField(max_length=2, null=True, description="性别")
    real_name = fields.CharField(max_length=50, null=True, description="真实名称")
    is_active = fields.BooleanField(default=True, description="用户状态")
    avatar = fields.CharField(max_length=255, null=True, description="头像图片存储地址")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")
    job_seeker_status = fields.IntEnumField(
        JobSeekerStatus, 
        default=JobSeekerStatus.UNEMPLOYED_SEEKING,
        null=True, 
        description="求职者状态(1:离职正在找工作 2:在职急需工作 3:在职找工作 4:在职没有找工作的打算)"
    )
    
    def verify_password(self, plain_password: str) -> bool:
        return pwd_context.verify(plain_password, self.password)

    @classmethod
    async def create_user(cls, username: str, password: str, email: str, **kwargs):
        # 使用 bcrypt 加密密码
        hashed_password = pwd_context.hash(password)
        return await cls.create(
            username=username,
            password=hashed_password,
            email=email,
            **kwargs
        )
    

class Resume(models.Model):
    "用户简历模型"
    user = fields.ForeignKeyField('models.User', related_name='resumes', on_delete=fields.CASCADE, description="关联用户")
    resume_url = fields.CharField(max_length=255, description="简历存储地址")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table = "user_resumes"
        table_description = "用户简历存储表"

 
class Expectation(models.Model):
    "用户求职期望模型"
    user = fields.ForeignKeyField('models.User', related_name='expectations', on_delete=fields.CASCADE, description="关联用户")
    job_name = fields.CharField(max_length=255, description="期望职位名称")
    money = fields.DecimalField(max_digits=10, decimal_places=2, description="期望薪资")
    place = fields.CharField(max_length=255, description="期望工作地点")
    industry = fields.CharField(max_length=255, description="期望行业")
    created_at = fields.DatetimeField(auto_now_add=True, description="创建时间")
    updated_at = fields.DatetimeField(auto_now=True, description="更新时间")

    class Meta:
        table = "user_expectations"
        table_description = "用户求职期望表"
